package com.huanuo.internetofthings.service.impact.statistics;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.exception.PendingException;

import com.huanuo.internetofthings.bo.impact.ImpactMonthStatistics;
import com.huanuo.internetofthings.bo.user.UserInfo;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceStatisticsReqDto;
import com.huanuo.internetofthings.enums.ImpactDeviceStatisticsType;
/**
 * 统计用户所在组  设备开工时长月度分布情况
 * @author xiebin
 *
 */
@Service
public class ImpactDeviceStartingTimeStatisticsService extends AbstractImpactDeviceStatisticsService {

	@Override
	public ImpactMonthStatistics getImpactMonthStatistics(UserInfo userInfo) throws PendingException {
		ImpactMonthStatistics impactMonthStatistics = new ImpactMonthStatistics();
		impactMonthStatistics.setGroupNo(userInfo.getGroupNo());
		int year = getYear();
		impactMonthStatistics.setYear(year);
		return impactMonthStatistics;
	}

	@Override
	public ResponseDto<?> calculationStatistics(List<ImpactMonthStatistics> impactMonthStatisticsList,UserInfo userInfo)
			throws PendingException {
		// 按月分组
		Map<Integer, List<ImpactMonthStatistics>> mapGroupbyMonth = impactMonthStatisticsList.stream()
				.collect(Collectors.groupingBy(ImpactMonthStatistics::getMonth));
		//初始化统计结果
		Map<Integer, Integer> returnMap = getSortMap(Integer.class);
		for (int m = 1; m <= 12; m++) {
			returnMap.put(m, 0);
		}
		//设置统计结果
		Set<Integer> monthSet = mapGroupbyMonth.keySet();
		for (Integer mm : monthSet) {
			Integer monthStartingTime = mapGroupbyMonth.get(mm).stream().mapToInt(ImpactMonthStatistics::getStartingTimeTotal).sum();
			returnMap.put(mm, monthStartingTime);
		}
		return new ResponseDto<>(returnMap);
	}

	@Override
	public boolean supports(ImpactDeviceStatisticsReqDto s) throws PendingException {
		return ImpactDeviceStatisticsType.Month_StartingTimeDistribution.equals(s.getImpactDeviceStatisticsType());
		
	}

}